//
//  orderSubmitViewController.m
//  AnnCar_user
//
//  Created by LiBenCan on 17/10/24.
//  Copyright © 2017年 LiBenCan. All rights reserved.
//

#import "orderSubmitViewController.h"
#import "OrderSubmitCell.h"
#import "OrderChooseCell.h"
#import "CartView.h"
#import "StartCell.h"
#import "ChooseTitleView.h"
#import "ComboViewController.h"
#import "chooseBtnView.h"
#import "CarMangerViewController.h"
#import "PersonViewController.h"
#import "AdressMangerViewController.h"
static NSString *const StartCellID = @"StartCellID";
static NSString *const OrderSubmitCellID = @"OrderSubmitCellID";
static NSString *const OrderChooseCellD = @"OrderChooseCellID";
@interface orderSubmitViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)chooseBtnView *selectBtn;//选择的btb
@property (nonatomic,strong)chooseBtnView *selectPayBtn;//选择的支付
@property (nonatomic,strong)NSArray *imageArr;//标题图片数组
@property (nonatomic,strong)NSArray *titleArr;//标题数组
@property (nonatomic,strong)CartView *cartView;//购物车
@property (nonatomic,assign)BOOL isChooseWoker;//是否选定师傅
@property (nonatomic,strong)NSMutableArray *dataArr;//假数据
@end

@implementation orderSubmitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isChooseWoker = NO;
    [self initTitleWithTitle:@"提交订单"];
    [self initLeftBarButtonWithImage:@"back"];
    [self.view addSubview:self.tableV];
    [self.view addSubview:self.cartView];
}
//初始化标题数组
-(NSArray *)titleArr
{
    if(!_titleArr){
        _titleArr = @[@[@"服务车辆",
                        @"车辆位置",
                        @"联系人",
                        @"提车时间",
                        @"备注信息",
                        @"指定师傅",
                       ],
                      @[@"手机号码",
                        @"支付方式",
                        @""]
                      ];
    }
    return _titleArr;
}
//初始化标题数组
-(NSArray *)dataArr
{
    if(!_dataArr){
        _dataArr = @[@[@"宝马",
                       @"未来之光",
                       @"灿灿",
                       @"2017－10-26",
                       @"要准时",
                       @"指定师傅",
                       ].mutableCopy,
                     @[@"13311313131",
                       @"支付方式",
                       @""].mutableCopy
                      ].mutableCopy;
    }
    return _dataArr;
}
//创建tableView
-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64-45*screenW_6) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = backColor;
        _tableV.estimatedRowHeight = 50*screenW_6;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        [_tableV registerClass:[OrderSubmitCell class] forCellReuseIdentifier:OrderSubmitCellID];
        [_tableV registerClass:[OrderChooseCell class] forCellReuseIdentifier:OrderChooseCellD];
        [_tableV registerClass:[StartCell class] forCellReuseIdentifier:StartCellID];
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
//购物车
-(CartView *)cartView
{
    if (!_cartView) {
        _cartView = [[CartView alloc]initWithFrame:CGRectMake(0,SCREENH-64-45*screenW_6, SCREENW,45*screenW_6)];
    }
    return _cartView;
}
#pragma Mark TableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else  if(section == 1){
        return [self.titleArr[0] count];
    }else  if(section == 2 && _isChooseWoker){
        return [self.titleArr[1] count];
    }
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section <= 1){
        return 5*screenW_6;
    }
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {//洗车类型价钱
        OrderSubmitCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderSubmitCellID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setDataDic:nil];
        return cell;
    }
    else if (indexPath.section == 1) //洗车详细类容
    {
        OrderChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderChooseCellD forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell configCellTypeTitle:self.titleArr[indexPath.section-1][indexPath.row] Model:nil];
        cell.detailLab.hidden = NO;
        
        if (indexPath.row == 5) {
            cell.detailLab.hidden = YES;
            cell.arrowImageV.hidden = YES;
            //选择
            for (int i = 0; i<2; i++) {
                float x = 80*screenW_6 + 65*i;
                chooseBtnView *btnView = [[chooseBtnView alloc]initWithFrame:CGRectMake(x,0,65*screenW_6,49*screenW_6) Title:i == 0?@"否":@"是" NomolImageV:@"circle" SelectImageV:@"circle_select" IsSelect:NO];
                btnView.tag = i;
                [cell.contentView addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnTap:)];
                [btnView addGestureRecognizer:tap];
            }
        }else if (indexPath.row == 4)
        {
           cell.arrowImageV.hidden = YES;
        }
        return cell;
        
    }else
    {
        OrderChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderChooseCellD forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         [cell configCellTypeTitle:_isChooseWoker?self.titleArr[indexPath.section-1][indexPath.row]:@"支付方式" Model:nil];
        cell.arrowImageV.hidden = YES;
         if (indexPath.row >= 1) {
             
             cell.detailLab.hidden = YES;
             chooseBtnView *payBtnView = [[chooseBtnView alloc]initWithFrame:CGRectMake(80,0,65*screenW_6,49*screenW_6) Title:indexPath.row == 1?@"余额支付":@"微信支付" NomolImageV:@"circle" SelectImageV:@"circle_select" IsSelect:NO];
             payBtnView.tag = indexPath.row;
             UITapGestureRecognizer *payTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(payBtnTap:)];
             [payBtnView addGestureRecognizer:payTap];
             [cell.contentView addSubview:payBtnView];
         }
        
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
       [self.navigationController pushViewController:[ComboViewController new] animated:YES];
    }else if (indexPath.section == 1)
    {
        if (indexPath.row == 0) {//选车
            CarMangerViewController *VC = [CarMangerViewController new];
            [self.navigationController pushViewController:VC animated:YES];
            VC.selectCarBlock = ^(NSString *backStr){
                [_dataArr[0] replaceObjectAtIndex:0 withObject:backStr];
                 [_tableV reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
            };
            
        }else if (indexPath.row == 1) {//选地址
            AdressMangerViewController *VC = [AdressMangerViewController new];
            [self.navigationController pushViewController:VC animated:YES];
            VC.selectAddressBlock = ^(NSString *backStr){
                [_dataArr[0] replaceObjectAtIndex:1 withObject:backStr];
                [_tableV reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
            };
            
        }else if (indexPath.row == 2) {//选人
            PersonViewController *VC = [PersonViewController new];
            [self.navigationController pushViewController:VC animated:YES];
            VC.selectPersonBlock = ^(NSString *backStr){
                [_dataArr[0] replaceObjectAtIndex:2 withObject:backStr];
                [_tableV reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
            };
        }
    }
}
//是否选择师傅
-(void)btnTap:(UITapGestureRecognizer *)tap
{
    _isChooseWoker = (tap.view.tag == 0)?NO:YES;
    _selectBtn.isChoose = NO;
    _selectBtn.leftImagV.image = [UIImage imageNamed:_selectBtn.isChoose?@"circle_select":@"circle"];
    chooseBtnView *btnView = (id)tap.view;
    btnView.isChoose = YES;//反选按钮
    btnView.leftImagV.image = [UIImage imageNamed:btnView.isChoose?@"circle_select":@"circle"];
    _selectBtn = btnView;
    [_tableV reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
    
}
//支付的选择
-(void)payBtnTap:(UITapGestureRecognizer *)tap
{
    _selectPayBtn.isChoose = NO;
    _selectPayBtn.leftImagV.image = [UIImage imageNamed:_selectPayBtn.isChoose?@"circle_select":@"circle"];
    chooseBtnView *btnView = (id)tap.view;
    btnView.isChoose = YES;//反选按钮
    btnView.leftImagV.image = [UIImage imageNamed:btnView.isChoose?@"circle_select":@"circle"];
    _selectPayBtn = btnView;

}

@end
